<template>
  <div>
    <!-- 顶部导航栏 -->
    <van-nav-bar
      title="常用地址"
      left-text="返回"
      right-text="完成"
      left-arrow
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <!-- 地址列表 -->
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    <!-- 新增地址弹出层 -->
    <van-popup v-model="isNewAddressShow" position="bottom" :style="{ height: '100%' }">
      <new-address @close="isNewAddressShow=false"></new-address>
    </van-popup>
  </div>
</template>

<script>
import axios from 'axios'
import NewAddress from './new-address'
export default {
  name: 'UsualAddress',
  components: {
    NewAddress
  },
  props: {
    addressId: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      chosenAddressId: '1',
      list: [
        {
          id: '1',
          name: '张三',
          tel: '13000000000',
          address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
          isDefault: true
        },
        {
          id: '2',
          name: '李四',
          tel: '1310000000',
          address: '浙江省杭州市拱墅区莫干山路 50 号'
        }
      ],
      isNewAddressShow: false
    }
  },
  computed: {},
  watch: {},
  created () {
    this.gitAddress()
  },
  mounted () {

  },
  methods: {
    gitAddress () {
      axios.get('http://192.168.14.225:8888/dataInfo', {
        params: {
          id: this.addressId
        }
      })
        .then(res => {
          res.data.forEach(x => {
            console.log(x.consignee)
            x.consignee.forEach(y => {
              console.log(y)
              const consignees = [
                {
                  id: this.addressId,
                  name: y.name,
                  tel: y.mobile,
                  address: y.address
                }
              ]
              console.log(consignees)
              this.list.unshift(...consignees)
            })
          })
        })
        .catch(err => {
          console.error(err)
        })
    },
    onAdd () {
      this.isNewAddressShow = true
    },
    onEdit (item, index) {
      console.log(item)
    },
    onClickRight () {
    }
  }
}
</script>

<style scoped lang="less">
 /deep/.van-address-list {
   margin-top: 92px;
 }
</style>
